import { _decorator, Component, Event, Label, Node, Prefab, SkeletalAnimation, Sprite, SpriteFrame, tween, UIOpacity, v3, Vec3 } from 'cc';
import { MainUI } from './MainUI';
import { PoolManager } from 'db://assets/Scripts/Framework/Managers/PoolManager';
import { SkinItemUI } from './SkinItemUI';
import { GameDataManager } from '../GameDataManager';
import NodeUtil from 'db://assets/Scripts/Framework/Utils/NodeUtil';
import { UIManager } from 'db://assets/Scripts/Framework/Managers/UIManager';
import Banner from 'db://assets/Scripts/Banner';
import { EasingType } from 'db://assets/Scripts/Framework/Utils/TweenUtil';
import { BundleManager } from 'db://assets/Scripts/Framework/Managers/BundleManager';
import { ProjectEventManager, ProjectEvent } from 'db://assets/Scripts/Framework/Managers/ProjectEventManager';
const { ccclass, property } = _decorator;

@ccclass('GamerUI')
export class GamerUI extends Component {

    private static instance: GamerUI;
    public static get Instance(): GamerUI {
        return this.instance;
    }

    @property(Label)
    equipLabel: Label;

    @property(Node)
    videoBtn: Node;

    @property(Node)
    content: Node;

    @property(Node)
    rtSprite: Node;

    @property(Node)
    unlock: Node;

    @property(Prefab)
    skinPrefab: Prefab;

    @property([Node])
    typeBtns: Node[] = [];

    type = 0;
    num = 0;

    useSkins: number[];
    allSkin: number[][];
    skinPieces: number[][];

    protected onLoad(): void {
        GamerUI.instance = this;
        MainUI.Instance._InitRotate(this.rtSprite);
    }

    protected onEnable(): void {
        this.UpdateStorage(true);
        this.SwitchAdornmentType(0);
        ProjectEventManager.emit(ProjectEvent.弹出窗口);
    }

    start() {

    }

    update(deltaTime: number) {

    }

    Back() {
        this.node.active = false;
        MainUI.Instance._InitPlayer();
    }

    SwitchAdornmentType(arg: Event | number) {
        this.num = -1;
        this._InitModel();
        if (arg instanceof Event) {
            let target: Node = arg.target;
            arg = target.parent.getSiblingIndex();
        }
        this.type = arg;
        for (let i = 0; i < this.typeBtns.length; i++) {
            const element = this.typeBtns[i].getComponent(UIOpacity);
            element.opacity = i == arg ? 255 : 0;
        }
        let data = MainUI.Instance.items.Adorn[arg];
        let length = data.length;
        for (let i = 0; i < length; i++) {
            const element = data[i];
            let item = this.content.children[i];
            if (!item) item = PoolManager.GetNodeByPrefab(this.skinPrefab, this.content);
            let ui = item.getComponent(SkinItemUI);
            ui._Init(element);
            if (i == this.useSkins[this.type]) ui.Click();
        }
        if (arg != 0) {
            let item = this.content.children[length];
            if (!item) item = PoolManager.GetNodeByPrefab(this.skinPrefab, this.content);
            let ui = item.getComponent(SkinItemUI);
            ui._Init();
            if (this.useSkins[this.type] == -1) ui.Click();
            length++;
        }
        for (let i = length; i < this.content.children.length; i++) {
            const element = this.content.children[i];
            PoolManager.PutNode(element);
        }
        MainUI.Instance.PlayClick();
    }

    UpdateStorage(force = false) {
        if (force || !(this.allSkin && this.skinPieces && this.useSkins)) {
            this.useSkins = GameDataManager.Instance.getArrayData("UseSkin");
            this.allSkin = GameDataManager.Instance.getArrayData("AllSkin");
            this.skinPieces = GameDataManager.Instance.getArrayData("SkinPieces");
        }
        else {
            GameDataManager.Instance.setArrayData("UseSkin", this.useSkins);
            GameDataManager.Instance.setArrayData("AllSkin", this.allSkin);
            GameDataManager.Instance.setArrayData("SkinPieces", this.skinPieces);
        }
        this._CheckUseful();
    }

    _CheckUseful() {
        this.videoBtn.active = this.num < this.skinPieces[this.type].length && this.skinPieces[this.type][this.num] < 20;
        this.equipLabel.string = (this.num < this.skinPieces[this.type].length && this.skinPieces[this.type][this.num] >= 20 && this.allSkin[this.type][this.num] != 1) ? "解锁" : "装备";
    }

    Choose(target: Node) {
        this.num = target.getSiblingIndex();
        for (let i = 0; i < this.content.children.length; i++) {
            const element = this.content.children[i];
            element.getComponent(SkinItemUI).choose.active = i == this.num;
        }
        this._CheckUseful();
        switch (this.type) {
            case 0:
                this._InitModel();
                break;
            case 1:
                this._InitHead();
                break;
            case 2:
                this._InitWing();
                break;
        }
        MainUI.Instance.PlayClick();
    }

    _InitModel() {
        if (this.num == -1) MainUI.Instance._InitPlayer();
        else {
            for (let i = 0; i < MainUI.Instance.gamers.children.length; i++) {
                const element = MainUI.Instance.gamers.children[i];
                element.active = i == this.num;
            }
            MainUI.Instance.model = MainUI.Instance.gamers.children[this.num];
            MainUI.Instance.curWeapon = NodeUtil.GetNode("Hand", MainUI.Instance.model);
            MainUI.Instance.headwears = NodeUtil.GetNode("toutao", MainUI.Instance.model);
            MainUI.Instance.wings = NodeUtil.GetNode("chibang", MainUI.Instance.model);
            if (MainUI.Instance.headwears.children[0]) PoolManager.PutNode(MainUI.Instance.headwears.children[0]);
            if (MainUI.Instance.wings.children[0]) PoolManager.PutNode(MainUI.Instance.wings.children[0]);
            MainUI.Instance._InitWeapon();
        }
    }

    _InitWing() {
        if (MainUI.Instance.wings.children[0]) PoolManager.PutNode(MainUI.Instance.wings.children[0]);
        if (this.num >= MainUI.Instance.items.Adorn[2].length) return;
        let wing = MainUI.Instance.items.Adorn[2][this.num];
        PoolManager.GetNodeByBundle("MainPack", wing.prefab, MainUI.Instance.wings);
    }

    _InitHead() {
        if (MainUI.Instance.headwears.children[0]) PoolManager.PutNode(MainUI.Instance.headwears.children[0]);
        if (this.num >= MainUI.Instance.items.Adorn[1].length) return;
        let head = MainUI.Instance.items.Adorn[1][this.num];
        PoolManager.GetNodeByBundle("MainPack", head.prefab, MainUI.Instance.headwears);
    }

    Use() {
        if (this.num >= 0 && this.num < this.allSkin[this.type].length && this.allSkin[this.type][this.num] == 0) {
            this.allSkin[this.type][this.num] = 1;
            this._ShowUnlock();
            this.UpdateStorage();
        }
        else {
            this.useSkins[this.type] = this.num > this.allSkin[this.type].length - 1 ? -1 : this.num;
            this.UpdateStorage();
            for (let i = 0; i < this.content.children.length; i++) {
                const element = this.content.children[i].getComponent(SkinItemUI);
                element.use.active = i == this.num;
            }
            UIManager.ShowTip("已装备!");
        }
        MainUI.Instance.PlayClick();
    }

    VideoUnlock() {
        let x = this;
        Banner.Instance.ShowVideoAd(() => {
            x.skinPieces[x.type][x.num]++;
            x.UpdateStorage();
            let item = x.content.children[x.num].getComponent(SkinItemUI);
            item._Init(item.data);
            let data = MainUI.Instance.items.Adorn[x.type][x.num];
            UIManager.ShowTip(`获得${data.name}碎片一个`);
        })
        MainUI.Instance.PlayClick();
    }

    _ShowUnlock() {
        let item = MainUI.Instance.items.Adorn[this.type][this.num];
        let sprite = this.unlock.children[2].getComponent(Sprite);
        BundleManager.GetBundle("MainPack").load(item.spriteFrame, SpriteFrame, (err, data) => { sprite.spriteFrame = data; });
        let label = this.unlock.children[3].children[0].getComponent(Label);
        label.string = item.name;
        tween(this.unlock)
            .to(0.5, { scale: Vec3.ONE }, { easing: EasingType.backOut })
            .start();
        MainUI.Instance.PlayBonus();
    }

    CloseUnlock() {
        tween(this.unlock)
            .to(0.5, { scale: Vec3.ZERO }, { easing: EasingType.backIn })
            .start();
        MainUI.Instance.PlayClick();
    }

}


